﻿using DataGridViewAutoFilter;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using FunnyArts.Enterprise.Business.ViewMode;
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using LicenseContext = OfficeOpenXml.LicenseContext;

namespace FunnyArts.Enterprise.Business
{
    public partial class FormMain : Form
    {

        private string dataFilePath {
            get {
                string path= Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "File");
                if (!Directory.Exists(path))
                    Directory.CreateDirectory(path);
                return path;
            }
        }
        private List<CustomModel> data = new List<CustomModel>();

       

        public FormMain()
        {
            InitializeComponent();
            dateTimePicker1.Value = new DateTime(DateTime.Now.Year, 1, 1);
            dateTimePicker2.Value = DateTime.Now;
        }

        private void simpleButton6_Click(object sender, EventArgs e)
        {
            string beginTimeStr = dateTimePicker1.Value.ToString("yyyy-MM-dd") +" "+ dateTimePicker3.Value.ToString("HH:mm:ss");
            string endTimeStr = dateTimePicker2.Value.ToString("yyyy-MM-dd") +" "+ dateTimePicker4.Value.ToString("HH:mm:ss");
            DateTime beginTime = DateTime.Parse(beginTimeStr);
            DateTime endTime = DateTime.Parse(endTimeStr);

            var fitterResult=data.Where(w =>
            {
                if (DateTime.TryParse(w.cysj, out DateTime cysjResult))
                {
                    if (DateTime.Compare(cysjResult, beginTime) >= 0 && DateTime.Compare(cysjResult, endTime) <= 0)
                        return true;
                    else
                        return false;
                }
                else
                    return false;
            });
            BindingSource bindingSource = new BindingSource();
            bindingSource.DataSource = fitterResult;

            gridControl1.DataSource = bindingSource;
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "Excel Files|*.xls;*.xlsx";

                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    string filePath = openFileDialog.FileName;
                    data = GetDataTableFromExcel(filePath);

                    if (data != null)
                    {
                        // 初始化BindingSource并设置其数据源为DataTable
                        BindingSource bindingSource = new BindingSource();
                        bindingSource.DataSource = data;

                        gridControl1.DataSource = bindingSource;
                        
                    }
                    else
                    {
                        MessageBox.Show("无法读取Excel文件。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        private List<CustomModel> GetDataTableFromExcel(string path)
        {
            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
            FileInfo fileInfo = new FileInfo(path);
            using (ExcelPackage package = new ExcelPackage(fileInfo))
            {
                ExcelWorksheet worksheet = package.Workbook.Worksheets[0];

                Type type=typeof(CustomModel);
                var propertyInfos=type.GetProperties();

                // 添加行数据
                for (int rowIndex = 2; rowIndex <= worksheet.Dimension.End.Row; rowIndex++)
                {
                    CustomModel customModel = new CustomModel();
                    for (int colIndex = 2; colIndex <= worksheet.Dimension.End.Column; colIndex++)
                    {
                        propertyInfos[colIndex - 2].SetValue(customModel, worksheet.Cells[rowIndex, colIndex].Text);
                    }
                    data.Add(customModel);
                }

                return data;
            }
        }

        private void bandedGridView1_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            LoginForm loginForm = new LoginForm();
            var dialogResult=loginForm.ShowDialog();
            if (dialogResult == DialogResult.Yes)
            {
                label3.Text = loginForm.code;
                label4.Text = loginForm.name;
            }
        }

        private void comboBoxEdit1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string fileName = $"{(sender as ComboBoxEdit).Text}.xlsx";
            string fileFullName = Path.Combine(dataFilePath, fileName);
            if (!File.Exists(fileFullName))
            {
                MessageBox.Show("没有找到该电厂对应的数据!");
                return;
            }
            data = new List<CustomModel>();
            data =GetDataTableFromExcel(fileFullName);
            if (data != null)
            {
                gridControl1.DataSource = null;
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = data;

                gridControl1.DataSource = bindingSource;
            }
        }
         //左侧列表点击给于弹窗提示
        private void navBarItem1_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            MessageBox.Show("您无此操作权限！请联系系统管理员", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }
}
